package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.BaseService;
import com.nexus.core.sys.entity.SysOrgUser;
import com.nexus.service.sys.mapper.SysOrgUserMapper;
import com.nexus.core.sys.dto.SysOrgUserDTO;
import com.nexus.service.sys.service.mapstruct.SysOrgUserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 机构用户Service
 * @author 路正宁
 * @version 2023-05-10
 */
//@DS("zxhy_platform_db")
//@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysOrgUserService extends BaseService<SysOrgUserMapper, SysOrgUser,SysOrgUserDTO,SysOrgUserWrapper> {

	@Autowired
	SysOrgUserWrapper sysOrgUserWrapperImpl;
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public SysOrgUserDTO findById(String id) {
		return baseMapper.findById ( id );
	}

	/**
	 * 查询业务经理列表
	 * @Date 2025/6/9 10:25
	 * @Author luzhengning
	 **/
	public List<SysOrgUserDTO> findHandleByList() {
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.eq ("role.en_name", "yewujingli" ); // 排除已经删除
		queryWrapper.eq ("a.deleted", 0 ); // 排除已经删除
		queryWrapper.eq ("user.deleted", 0 ); // 排除已经删除
		return  baseMapper.findList (new Page<>(1,5000), queryWrapper).getRecords();
	}

	/**
	 * 自定义分页检索
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	public IPage <SysOrgUserDTO> findPage(Page <SysOrgUserDTO> page, QueryWrapper queryWrapper) {
		queryWrapper.eq ("a.deleted", 0 ); // 排除已经删除
		queryWrapper.eq ("user.deleted", 0 ); // 排除已经删除
		return  baseMapper.findList (page, queryWrapper);
	}

	/**
	 * 重复查询
	 * @Date 2023/5/10 16:54
	 * @Author luzhengning
	 **/
	public boolean has(SysOrgUser orgUser){
		//重复查询
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.eq("org",orgUser.getOrg());
		queryWrapper.eq("user",orgUser.getUser());
		List list = this.list(queryWrapper);
		if(ObjectUtils.isEmpty(list)||list.size()==0){
			return false;
		}
		return true;
	}

	/**
	 * 删除用户关联
	 * @param userIds
	 * @return
	 */
	public boolean deleteByUserId(List<String> userIds){
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.in("user",userIds);
		return this.remove(queryWrapper);
	}
	/**
	 * 删除机构关联
	 * @Date 2023/9/6 9:49
	 * @Author luzhengning
	 **/
	public boolean deleteByOrgIds(List<String> orgIds){
		if(ObjectUtils.isEmpty(orgIds))return false;
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.in("org",orgIds);
		return this.remove(queryWrapper);
	}

	/**
	 * 根据指定条件删除机构用户关联
	 *
	 * @param orgId 组织机构ID
	 * @param userId 用户ID
	 * @return 用户组织关联信息
	 */
	public boolean deleteByOrgAndUser(String orgId, String userId) {
		LambdaUpdateWrapper<SysOrgUser> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(SysOrgUser::getOrg, orgId);
		updateWrapper.eq(SysOrgUser::getUser, userId);
		updateWrapper.set(SysOrgUser::getDeleted, 1);
		return update(updateWrapper);
	}

}
